﻿using Common;
using DataLibrary;
using System;
using System.Linq;
using System.Windows.Forms;

namespace TicketSystem
{
    public partial class Form_TravelUpdate : Form
    {
        public Form_TravelUpdate()
        {
            InitializeComponent();
        }
        public string DepartmentName;
        private TicketSystemEntities tse = new TicketSystemEntities();
        private PublicMethod pm = new PublicMethod();
        private void Form_TravelUpdate_Load(object sender, EventArgs e)
        {
            var departmentTypes = tse.DepartmentTypes.ToList();
            comType.DataSource = departmentTypes;
            comType.DisplayMember = "TypeName";
            comType.ValueMember = "DepartmentTypeID";
            var department = tse.Departments.Where(u => u.DepartmentName==DepartmentName).FirstOrDefault();
            this.comType.SelectedValue = department.DepartmentTypeID;
            this.txtName.Text = department.DepartmentName;
            this.txtNickName.Text = department.DepartmentNickName;
            this.txtPhone.Text = department.ContactNumber;
            this.txtFax.Text = department.Fax;
            this.txtAddress.Text = department.Address;
            this.txtBank.Text = department.Bank;
            this.txtAccount.Text = department.AccountNumber;
            this.txtTax.Text = department.TaxNumber;
            if (department.CanSign == 0)
            {
                this.checkSign.Checked = true;
            }
            else
            {
                this.checkSign.Checked = false;
            }
        }

        private void btn_Save_Click(object sender, EventArgs e)
        {
            if (pm.IsTelephone(this.txtPhone.Text))
            {
                if (pm.IsAccountNumber(this.txtAccount.Text))
                {
                    var department = tse.Departments.Where(u => u.DepartmentName == DepartmentName).FirstOrDefault();
                    if (tse.Departments.Where(u => u.DepartmentName == this.txtName.Text).Count() > 0)
                    {
                        MessageBox.Show("该旅行社已存在！");
                        return;
                    }
                    department.DepartmentTypeID = Convert.ToInt32(this.comType.SelectedValue);
                    department.DepartmentName = this.txtName.Text;
                    department.DepartmentNickName = this.txtNickName.Text;
                    department.ContactNumber = this.txtPhone.Text;
                    department.Fax = this.txtFax.Text;
                    department.Address = this.txtAddress.Text;
                    department.Bank = this.txtBank.Text;
                    department.AccountNumber = this.txtAccount.Text;
                    department.TaxNumber = this.txtTax.Text;
                    if (this.checkSign.Checked)
                    {
                        department.CanSign = 0;
                    }
                    else
                    {
                        department.CanSign = 1;
                    }
                    tse.Configuration.ValidateOnSaveEnabled = false;
                    try
                    {
                        var result = tse.SaveChanges();
                        if (result >= 1)
                        {
                            this.Close();
                        }
                        else
                        {
                            MessageBox.Show("你当前没有做任何修改");
                            this.Close();
                        }
                    }
                    catch
                    {
                        MessageBox.Show("更新失败了");
                    }
                  
                }
                else
                {
                    MessageBox.Show("银行卡号输入有误");
                }
            }
            else
            {
                MessageBox.Show("手机号输入有误");
            }
        }

        private void btn_Cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
